<?php
namespace App\Repositories;

use DB;

class RecruitRepository extends BaseRepository
{

    protected $service;
    public function __construct()
    {
    }



    public function index($data, $student_id, $size = '')
    {
    }
    public function signUp($data,$service,$extension,$my_extension_id)
    {
        $commission = $this->getSet($data['school_id'],2);
        if($data['prepay_money'] != $commission->prepay_money) return false;
        if(empty($extension))
        {
            $data['banxing'] = $service->classcurr;
            $data['price'] = $service->price;
            $data['handle_money'] = $service->price;
            $data['commission'] = $commission->privilege_money;
            if($my_extension_id >0 )
            {
                DB::table('extensions')->where('id',$my_extension_id)->update(['issign'=>1]);
            }
            return DB::table('yy_distribution_apply')->insertGetId($data);
        }else{
            $superior_money = ($commission->superior_money/100) * $commission->prepay_money;
            $superior_superior_money = ($commission->superior_superior_money/100) * $commission->prepay_money;
            $data['banxing'] = $service->classcurr;
            $data['price'] = $service->price;
            $data['handle_money'] = $service->price;
            $data['superior_money'] = $superior_money;
            $data['superior_name'] = $extension->nickname;
            $data['privilege_money'] = $commission->privilege_money;
            if($extension->p_id >0)
            {
                $data['superior_superior_name'] = DB::table('extensions')->where('id',$extension->p_id)->value('nickname');
                $data['commission'] = $superior_money + $superior_superior_money;
            }else{
                $data['commission'] = $superior_money;
            }
            $data['superior_superior_money'] = $superior_superior_money;
            $distribution_id = DB::table('yy_distribution_apply')->insertGetId($data);
            if($my_extension_id >0 && $distribution_id)
            {
                DB::table('extensions')->where('id',$my_extension_id)->update(['issign'=>1]);
            }
            return $this->extensionCommission($extension,$superior_money,$superior_superior_money,$data['serial_number'],$distribution_id,$data['img'],$data['nickname']);
        }
    }
    public function extensionCommission($extension, $superior_money,$superior_superior_money,$serial_number,$distribution_id,$img,$nickname)
    {
        if(empty($extension)) return false;
        if($extension->p_id == 0)
        {
            DB::beginTransaction();
            try
            {
                $data['extension_id'] = $extension->id;
                $data['serial_number'] = $serial_number;
                $data['money'] = $superior_money;
                $data['status'] = 0;
                $data['distribution_id'] = $distribution_id;
                if(!empty($img))$data['img'] = $img;
                if(!empty($nickname))$data['nickname'] = $nickname;
                DB::table('extension_records')->insert($data);
                DB::table('extensions')->where('id',$extension->id)->update(['frozen_commission'=>$extension->frozen_commission + $superior_money,'sign_count'=>$extension->sign_count + 1]);
                DB::commit();
                return true;
            }
            catch(Exception $e)
            {
                DB::rollBack();
                return false;
            }
        }else{
            DB::beginTransaction();
            try
            {
                $data['extension_id'] = $extension->id;
                $data['serial_number'] = $serial_number;
                $data['money'] = $superior_money;
                $data['status'] = 0;
                $data['distribution_id'] = $distribution_id;
                if(!empty($img))$data['img'] = $img;
                if(!empty($nickname))$data['nickname'] = $nickname;
                DB::table('extension_records')->insert($data);
                DB::table('extensions')->where('id',$extension->id)->update(['frozen_commission'=>$extension->frozen_commission + $superior_money,'sign_count'=>$extension->sign_count + 1]);

                $data2['extension_id'] = $extension->p_id;
                $data2['serial_number'] = $serial_number;
                $data2['money'] = $superior_superior_money;
                $data2['status'] = 0;
                $data2['distribution_id'] = $distribution_id;
                if(!empty($img))$data2['img'] = $img;
                $data2['nickname'] = DB::table('extensions')->where('id',$extension->p_id)->value('nickname');
                DB::table('extension_records')->insert($data2);
                DB::table('extensions')->where('id',$extension->p_id)->update(['frozen_commission'=>$extension->frozen_commission + $superior_superior_money,'sign_count'=>$extension->sign_count + 1]);
                DB::commit();
                return true;
            }
            catch(Exception $e)
            {
                DB::rollBack();
                return false;
            }
        }
    }
    public function withdraw($extension_id,$money,$name,$tel)
    {
        $extension = DB::table('extensions')->where('id',$extension_id)->first();
        if(empty($extension)) return ['status'=>0,'message'=>'推广员id错误'];
        if($extension->commission < $money) return ['status'=>0,'message'=>'余额不足'];
        $set = $this->getSet($extension->school_id,6);
        if($money>$set->maximum || $money<$set->minimum)
        {
            return ['status'=>0,'message'=>"提现金额大于{$set->maximum},或小于{$set->minimum}"];
        }
        $todaySum = DB::table('withdraws')->where('created_at','>',date('Y-m-d 00:00:00'))->where('created_at','<',date('Y-m-d 23:59:59'))->value(DB::raw('sum(`money`)'));
        if( ($todaySum + $money) > $set->todaymaximum ) return ['status'=>0,'message'=>"今日提现金额以用完"];
        DB::beginTransaction();
        try
        {

            DB::table('extensions')->where('id',$extension_id)->update(['commission'=>($extension->commission-$money),'withdrawal_money'=>($extension->withdrawal_money+$money)]);
            $data['money'] = $money;
            $data['extension_id'] = $extension_id;
            $data['nickname'] = $extension->nickname;
            $data['open_id'] = $extension->open_id;
            $data['status'] = 0;
            $data['uname'] = $name;
            $data['telphone'] = $tel;
            $data['school_id'] = $extension->school_id;
            DB::table('withdraws')->insert($data);
            DB::commit();
            return ['status'=>1];;
        }
        catch(Exception $e)
        {
            DB::rollBack();
            return ['status'=>0,'message'=>"未知错误"];
        }
    }
    public function getSet($school_id,$type)
    {
        $recruit_templet = DB::table('schools')->where('id',$school_id)->value('recruit_templet');
        if($recruit_templet  == 1 && $type != 4)
        {
            $info = DB::table('recruit_sets')->where('system',1)->where('type',$type)->first();
        }else
        {
            $info = DB::table('recruit_sets')->where('system',2)->where('school_id',$school_id)->where('type',$type)->first();
            if(empty($info))
            {
                $info = RecruitSet::where('system',1)->where('type',$type)->first();
            }
        }
        $json_data = !empty($info)?$info->data:null;
        if(!empty($json_data))
        {
            $json_data = json_decode($json_data);
        }
        return $json_data;
    }
    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

    }


    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }


}